import java.util.ArrayList;
import java.util.List;

/**
 * @Author wonghiu
 * @Date 2021/8/7 15:21
 * @Version 1.0.1
 */
public class qianxin2 {
    public static void main(String[] args) {
        System.out.println(new Solution().getMaximumResource(new int[][]{{0,6,0},{5,8,7},{0,9,0}}));
    }

    static class Solution {
        /**
         * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
         *
         *
         * @param grid int整型二维数组 为n*m 的二维数组
         * @return int整型
         */
        int resMaxCount = 0;
        int resCount = 0;
//        List<Integer> check = new ArrayList<>();
        public int getMaximumResource (int[][] grid) {
            // write code here

            for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid[0].length; j++) {
                    dfs(grid,i,j);
                }
            }
            return resMaxCount;
        }

        private void dfs(int[][] grid, int i, int j) {
//            System.out.println(check);
//            System.out.println(resCount);
            if (i<0 || j<0 || i>grid.length-1 || j>grid[0].length-1 || grid[i][j]==0){
                return;
            }

            resCount+=grid[i][j];
//            check.add(grid[i][j]);
            int temp = grid[i][j];

            if (resCount>resMaxCount){
                resMaxCount = resCount;
            }

            grid[i][j]=0;
            dfs(grid,i+1,j);
            dfs(grid,i-1,j);
            dfs(grid,i,j+1);
            dfs(grid,i,j-1);
            grid[i][j]=temp;
            resCount-=grid[i][j];
//            check.remove(check.size()-1);
        }
    }

}
